<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Zhilvyun_Api_Order
{

    public static function check($params)
    {
        $rtn = array(
            'code'=>410,
            'msg'=>''
        );
        if(!isset($params['partnerDealId']))
        {
            return $rtn;
        }
        $ticket_id = $params['partnerDealId'];
        $suit_info = Model_Spot_Suit::suit_info($ticket_id);

        $row = DB::select()->from('spot_ticket')
            ->where('id','=',$ticket_id)
            ->execute()->current();

        if(!$row||$row['status']!=3)
        {
            $rtn = array(
                'code'=>421,
                'msg'=>'产品已下线'
            );
            return $rtn;
        }
        if($params['totalPrice']!=$params['unitPrice']*$params['quantity'])
        {
            $rtn = array(
                'code'=>499,
                'msg'=>'产品价格异常'
            );
            return $rtn;
        }
        //游客信息
        $tourer_info = $params['visitors'];
        //产品套餐详情
        if($suit_info['fill_tourer_type']>0)
        {
            $need_items = explode(',',$suit_info['fill_tourer_items']);

            //需要一位游客信息
            if($suit_info['fill_tourer_type'] == 1)
            {
                if(count($tourer_info) == 0)
                {
                    $rtn = array(
                        'code'=>499,
                        'msg'=>'至少需要一个游客信息'
                    );
                    return $rtn;
                }
            }
            //需要全部游客信息
            if($suit_info['fill_tourer_type'] == 2)
            {
                if(count($tourer_info) != $params['quantity'])
                {
                    $rtn = array(
                        'code'=>499,
                        'msg'=>'需要全部游客信息'
                    );
                    return $rtn;
                }
            }
            //身份证格式检测
            if(in_array('mobile',$need_items))
            {

                $rtn = Model_Zhilvyun_Api_Tools::check_tourer_mobile($tourer_info);
                if(!$rtn['status'])
                {
                    $rtn = array(
                        'code'=>499,
                        'msg'=>'游客手机号格式不正确'
                    );
                    return $rtn;
                }
            }
            //身份证号检测
            if(in_array('cardnumber',$need_items))
            {
                $rtn = Model_Zhilvyun_Api_Tools::check_tourer_cardno($tourer_info);
                if(!$rtn['status'])
                {
                    $rtn = array(
                        'code'=>499,
                        'msg'=>'游客身份证号格式不正确'
                    );
                    return $rtn;
                }
            }
        }
        $rtn = array(
            'code'=>200,
            'msg'=>'success'
        );
        if(isset($params['travelDate']))
        {
            $use_date = $params['travelDate'];
            //使用时间判断
            if(strtotime($use_date)<strtotime(date('Y-m-d')))
            {
                $rtn = array(
                    'code'=>499,
                    'msg'=>'预订日期有误'
                );
                return $rtn;
            }
            //提前预订天数
            if($suit_info['day_before'])
            {
                $current_date =date('Y-m-d');
                $diff_day = Model_Zhilvyun_Api_Tools::diff_between_two_days($use_date,$current_date);
                if($diff_day < $suit_info['day_before'])
                {

                    $rtn = array(
                        'code'=>499,
                        'msg'=>'此门票需要提前'.$suit_info['day_before'].'天预订'
                    );
                    return $rtn;

                }
            }
            //xx小时之间预订
            if($suit_info['hour_before'])
            {
                $hour = intval(date('H'));
                if($hour > intval($suit_info['hour_before']))
                {
                    $rtn = array(
                        'code'=>499,
                        'msg'=>'此门票需要在'.$suit_info['hour_before'].'点之前预订'
                    );
                    return $rtn;
                }
            }
            //库存判断
            $ticket_storage =DB::select('number')->from('spot_ticket_price')
                ->where('ticketid','=',$ticket_id)
                ->and_where('day','=',strtotime($params['travelDate']))
                ->execute()
                ->current();
            if(!$ticket_storage)
            {
                $rtn = array(
                    'code'=>422,
                    'msg'=>'当前日期没有价格日历'
                );
                return $rtn;
            }
            $params = array(
                'ticket_id' => $ticket_id,
                'use_date' => $use_date,
                'number' => -$params['quantity']
            );
            $rtns = Model_Zhilvyun_Api_Ticket::storage($params);
            if(!$rtns)
            {
                //库存不足,预订失败
                $rtn = array(
                    'code'=>420,
                    'msg'=>'库存不足'
                );
                return $rtn;
            }
        }
        return $rtn;
    }

    /**
     * @desc 插入电子票
     * @param $params
     * @return array
     */
    public static function insert_eticket($order_id,$valid_start,$valid_end,$etickets,$distribute_app_id,$number = 1)
    {
       foreach($etickets as $eticket)
       {
           $data = array(
               'order_id' => $order_id,
               'valid_start_date' => $valid_start,
               'valid_end_date' => $valid_end,
               'eticket' => $eticket,
               'distribute_app_id' => $distribute_app_id,
               'number' => $number

           );
           DB::insert('member_order_eticket',array_keys($data))->values(array_values($data))->execute();
       }


    }

    //添加游客信息
    public static function add_tourer($order_id,$tourer_name,$tourer_mobile,$tourer_cardno)
    {
        for ($i = 0; isset($tourer_name[$i]); $i++)
        {
            $ar = array(
                'orderid' => $order_id,
                'tourername' => $tourer_name[$i],

            );
            if(isset($tourer_mobile[$i]))
            {
                $ar['mobile'] = $tourer_mobile[$i];
            }
            if(isset($tourer_cardno[$i]))
            {
                $ar['cardnumber'] = $tourer_cardno[$i];
                $ar['cardtype'] = '身份证';
            }

            $m = ORM::factory('member_order_tourer');
            foreach ($ar as $k => $v)
            {
                $m->$k = $v;
            }
            $m->save();
            $m->clear();
        }

    }

    public static function get_status_list()
    {
        $status_list = Model_Member_Order::$statusNames;
        $result = array();
        foreach ($status_list as $status_id => $status_name)
        {
            $result[] = array('id' => $status_id, 'name' => $status_name);
        }
        return $result;

    }

    /**
     * 订单查询
     * @param $condition
     * @return array
     */
    public static function query($params)
    {

        $page = $params['page'] ? $params['page'] : 1;
        $pagesize = $params['pagesize'] ? $params['pagesize'] : 10;
        $offset = ($page-1) * $pagesize;
        $query = DB::select()->from('member_order')->where('source','=',5);
        //关键词搜索
        if($params['keyword'])
        {
            $query->and_where('title','like','%'.$params['keyword'].'%');
        }
        //订单号
        if($params['ordersn'])
        {
            $query->and_where('ordersn','=',$params['ordersn']);
        }
        //订单状态
        if($params['status'])
        {
            $query->and_where('status','=',$params['status']);
        }

        $orders = $query->limit($pagesize)->offset($offset)->execute()->as_array();
        return $orders;

    }

    /**
     * @function 获取消费码
     * @return string
     */
    public static function get_eticket()
    {

        $eticket = "";

        while (true)
        {
            $a = range(0,9);
            for($i=0;$i<16;$i++){
                $b[] = array_rand($a);
            }
            $eticket = join('',$b);

            $check_sql = "SELECT id FROM `sline_member_order_eticket` WHERE eticket='{$eticket}'";
            $row = DB::query(1, $check_sql)->execute()->as_array();

            if (count($row) <= 0)
            {
                break;
            }
            sleep(1);
        }
        return $eticket;
    }

    /**
     * 获取订单游客信息
     * @param $order_id
     * @return array
     */
    public static function get_tourer($order_id)
    {
        $tourer = DB::select()->from('member_order_tourer')->where('orderid','=',$order_id)->execute()->as_array();
        $out = array();
        foreach($tourer as $t)
        {
            $out[] = array(
                'name' => $t['tourername'],
                'card_type' => $t['cardtype'],
                'cardnumber' => $t['cardnumber'],
                'mobile' => $t['mobile']
            );
        }
        return $out;
    }















}